package se.webbzon.boltzmann.editor.component.sequence.panel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import se.webbzon.boltzmann.game.event.GameEventEnum;
import se.webbzon.boltzmann.game.sequence.builder.event.GameEventRow;

public class GameEventRowPanel extends AbstractRowPanel {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 7005657518030054564L;

	// The combo boxes of this panel
	private final JComboBox<GameEventEnum> eventInput;
	
	// The argument panel
	private final JPanel argumentPanel;
	
	// The text fields of this panel
	private JTextArea[] arguments;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new condition row panel. **/
	public GameEventRowPanel(GameEventRow row) {
		super(row);
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		add(new JLabel("Event:"));
		eventInput = new JComboBox<GameEventEnum>(GameEventEnum.values());
		eventInput.setSelectedItem(row.getGameEvent());
		eventInput.addActionListener(new ActionListenerImpl());
		add(eventInput);
		
		argumentPanel = new JPanel();
		argumentPanel.setBorder(BorderFactory.createTitledBorder("Arguments:"));
		
		updateArguments(row.getGameEvent().arguments());
		add(argumentPanel);
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean apply(Collection<String> errorMessages) {
		GameEventRow row = (GameEventRow) getSequenceRow();
		GameEventEnum event = 
				(GameEventEnum) eventInput.getSelectedItem();
		row.setGameEvent(event);
		
		int args = Math.min(event.arguments(),arguments.length); 
		String[] arguments = new String[args];
		for (int i = 0; i < args; i++) {
			arguments[i] = this.arguments[i].getText();
		}
		row.setArguments(arguments);
		
		return true;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Updates the state of the argument input fields. **/
	private final void updateArguments(int arguments) {
		GameEventRow row = (GameEventRow) getSequenceRow();
		String[] args = row.getArguments();
		final int nonEmptyArguments = (args == null) ? 0 : args.length;
		
		argumentPanel.removeAll();
		argumentPanel.setLayout(new GridLayout(arguments,2));
		this.arguments = new JTextArea[arguments];
		for (int i = 0; i < arguments; i++) {
			if (i < nonEmptyArguments)
				this.arguments[i] = new JTextArea(args[i]);
			else
				this.arguments[i] = new JTextArea();
			argumentPanel.add(new JLabel("Argument " + i + ":"));
			argumentPanel.add(this.arguments[i]);
		}
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of an action listener. **/
	private final class ActionListenerImpl implements ActionListener {

		@Override public void actionPerformed(ActionEvent arg0) {
			int args = ((GameEventEnum) eventInput.getSelectedItem()).arguments();
			updateArguments(args);
		}
		
	}

}
